import { NotificationCocosSDKManager } from './../scripts/managers/SDK/NotificationCocosSDKManager';
import { LaunchConfig } from './LaunchConfig';
import { profiler } from 'cc';
import { _decorator, Component, assetManager, log, director, ProgressBar, Canvas, Node, UITransform, view, find, lerp, Label, v3 } from 'cc';
import { AssetManager } from 'cc';

const { ccclass, property } = _decorator;

/**
 * 首场景只能使用cocoscreator最为原生的api，因为此时还没有加载封装好的库
 */

const _v30 = v3();

@ccclass('LaunchCanvas')
export class LaunchCanvas extends Component {

    @property(ProgressBar)
    progressBar: ProgressBar = null;

    @property(Label)
    progressLabel: Label = null;

    public get maxProgressValue(): number {
        return LaunchConfig.launch.maxProgressValue;
    }

    private _curProgressValue: number = 0;
    public get curProgressValue(): number { return this._curProgressValue; }
    public set curProgressValue(v: number) {
        this.log('@@curProgressValue', v);
        this._curProgressValue = v;
        const progressValue = Math.min(LaunchConfig.launch.firstProgressRateMax, v / (this.maxProgressValue) * LaunchConfig.launch.firstProgressRateMax);
        this.progressBar.progress = progressValue;
        this.progressLabel.string = `LOADING...${Math.round(progressValue * 100)}%`;
    }

    async start() {

        profiler.hideStats();

        this.curProgressValue = 1;
        await this.loadBundle('scripts');

        this.curProgressValue = 2;
        let canvasHeight = this.getComponent(Canvas).getComponent(UITransform).height;
        globalThis.safeAreaScale = canvasHeight / view.getVisibleSize().height;
        globalThis.safeExtraHeight = view.getVisibleSize().height - canvasHeight;
        this.log('canvasHeight', canvasHeight, view.getVisibleSize(), globalThis.safeAreaScale);

        this.curProgressValue = 3;
        await this.loadBundle('ab_lobby');

        this.curProgressValue = 5;
        this.registerSystems();

        this.curProgressValue = 6;

        assetManager.getBundle('ab_lobby').loadScene('LobbyScene', null, (...args) => {
            this.curProgressValue++;
        }, (err, sceneAsset) => {
            log('实际资源数/设定资源数', this.curProgressValue, this.maxProgressValue);
            this.curProgressValue = 15;

            globalThis.AdsManager.registerSystem();

            director.runScene(sceneAsset, () => {
                log('<Launch>', sceneAsset.name, sceneAsset.nativeUrl, 'before loadscene');
            }, (error, scene) => {
                log('<Launch>', sceneAsset.name, sceneAsset.nativeUrl, 'onLaunched', error, scene);
            });
        });

    }

    loadBundle(name: string): Promise<AssetManager.Bundle> {
        return new Promise((resolve, rejected) => {
            assetManager.loadBundle(name, (err, data) => {
                if (err != null) {
                    console.error(err);
                    rejected(err);
                } else {
                    resolve(data);
                }
            });
        });
    }

    private registerSystems() {
        // 各种manager在此注册
        globalThis.TimeManager.registerSystem();
        globalThis.JsbSDKManager.registerSystem(); // 所有的SDKManager必须在这个后面
        globalThis.MiscSDKManager.registerSystem();
        globalThis.CloudStorageSDKManager.registerSystem();
        globalThis.ThinkingCocosSDKManager.registerSystem();
        globalThis.AppsFlyerSDKManager.registerSystem();
        globalThis.UserDataManager.registerSystem();
        globalThis.ScreenManager.registerSystem();
        globalThis.ResourceManager.registerSystem();
        globalThis.GameManager.registerSystem();
        globalThis.TaskManager.registerSystem();
        globalThis.UIManager.registerSystem();
        globalThis.BundleAssetsManager.registerSystem();
        globalThis.AudioManager.registerSystem();
        globalThis.LobbyManager.registerSystem();
        globalThis.HotUpdateManager.registerSystem();
        globalThis.ClipboardManager.registerSystem();
        globalThis.IapSDKManager.registerSystem();
        globalThis.AppLovinCocosSDKManager.registerSystem();
        globalThis.NotificationCocosSDKManager.registerSystem();
        globalThis.PlayFabManager.registerSystem();
    }

    log(...params: any[]) {
        log.apply(this, params);
    }

}


